//
//  MSMyFootprintViewController.m
//  SocietySale
//
//  Created by yumingwei on 2016/11/26.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSMyFootprintViewController.h"
#import "MSDetailFootprintTableVC.h"

@interface MSMyFootprintViewController ()
@property (nonatomic, strong) MSDetailFootprintTableVC *tableVC;
@property (nonatomic, assign) NSInteger footerIndex;
@end

@implementation MSMyFootprintViewController


- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    titleLable.text = @"我的足迹";
}

- (void)initUI{
    self.tableVC = [[MSDetailFootprintTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = self.contentView.bounds;
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
}

- (void)testDataFunc{
    MSFootprintModel *model = [[MSFootprintModel alloc] init];
    model.name = @"张三";
    model.memberLevel = @"2";
    model.time = @"2016.9.15";
    model.introduction = @"杭州地铁二号线目前极影施工阶段杭州地铁二号线目前极影施工阶段";
    model.goodNum = @"25";
    model.badNum = @"13";
    model.isSelf = NO;
    model.isGoodEd = NO;
    model.isBadEd = NO;
    //http://7xlovk.com2.z0.glb.qiniucdn.com/exchange/upload/roomPhoto/20161209/148127216343372.jpeg
    NSArray *imagearray = @[@"example.jpg",
                            @"5.jpg",
                            @"6.jpg",
                            @"7.jpg",
                            @"6.jpg"];
//    NSArray *imagearray = @[@"http://7xlovk.com2.z0.glb.qiniucdn.com/exchange/upload/roomPhoto/20161209/148127216343372.jpeg",
//                            @"http://7xlovk.com2.z0.glb.qiniucdn.com/exchange/upload/roomPhoto/20161209/148127216343372.jpeg",
//                            @"http://7xlovk.com2.z0.glb.qiniucdn.com/exchange/upload/roomPhoto/20161209/148127216343372.jpeg",
//                            @"http://7xlovk.com2.z0.glb.qiniucdn.com/exchange/upload/roomPhoto/20161209/148127216343372.jpeg",
//                            @"http://7xlovk.com2.z0.glb.qiniucdn.com/exchange/upload/roomPhoto/20161209/148127216343372.jpeg"];
    NSMutableArray* bannerArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i =0; i<imagearray.count; i++) {
        propertyPicture* item = [[propertyPicture alloc] init];
        item.path = imagearray[i];
        [bannerArray addObject:item];
    }
    model.imageArray = bannerArray;
    
    MSFootprintModel *model2 = [[MSFootprintModel alloc] init];
    model2.name = @"王峰";
    model2.memberLevel = @"2";
    model2.time = @"2016.9.15";
    model2.introduction = @"上海浦东世和空港中心商业住房项目";
    model2.goodNum = @"5";
    model2.badNum = @"3";
    model2.isSelf = NO;
    model2.isGoodEd = NO;
    model2.isBadEd = NO;
    NSMutableArray* bannerArray2 = [[NSMutableArray alloc] initWithCapacity:0];
    model2.imageArray = bannerArray2;
    
    MSFootprintModel *model3 = [[MSFootprintModel alloc] init];
    model3.name = @"王峰";
    model3.memberLevel = @"2";
    model3.time = @"2016.9.15";
    model3.introduction = @"上海浦东世和空港中心商业住房项目";
    model3.goodNum = @"5";
    model3.badNum = @"3";
    model3.isSelf = NO;
    model3.isGoodEd = NO;
    model3.isBadEd = NO;
    model3.imageArray = bannerArray2;
    
    MSFootprintModel *model4 = [[MSFootprintModel alloc] init];
    model4.name = @"王峰";
    model4.memberLevel = @"2";
    model4.time = @"2016.9.15";
    model4.introduction = @"上海浦东世和空港中心商业住房项目";
    model4.goodNum = @"5";
    model4.badNum = @"3";
    model4.isSelf = NO;
    model4.isGoodEd = NO;
    model4.isBadEd = NO;
    model4.imageArray = bannerArray;
    
//    self.tableVC.dataArray = [@[model,model2,model4,model3] mutableCopy];
    self.tableVC.pageNum = 1;
    [self request:NO];
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"MyFoot"]!=nil){
            if (responseDic[@"state"]!=nil){
                self.tableVC.totalPagesNum = [responseDic[@"totalPage"] intValue];
                if (self.tableVC.pageNum == 1) {
                    self.tableVC.dataArray = nil;
                }
                NSMutableArray *dataArrray = [[NSMutableArray alloc] initWithArray:self.tableVC.dataArray];
                [dataArrray addObjectsFromArray:responseDic[@"MyFoot"]];
                self.tableVC.dataArray = dataArrray;
            }
            else{
                self.tableVC.dataArray = self.tableVC.dataArray;
            }
        }
        if (responseDic[@"DeleteFooter"]!=nil){
            [self.tableVC.dataArray removeObjectAtIndex:_footerIndex];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_footerIndex inSection:0];
            [self.tableVC.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
//            [self performSelector:@selector(reloadTableView) withObject:nil afterDelay:1];
            [self.tableVC.tableView reloadData];
        }
        if (responseDic[@"GoodFooter"]!=nil){
            MSFootprintModel *model = self.tableVC.dataArray[_footerIndex];
            model.isGoodEd = YES;
//            model.goodNum = [NSString stringWithFormat:@"%ld",[model.goodNum integerValue]+1];
            [self.tableVC.tableView reloadData];
        }
        if (responseDic[@"BadFooter"]!=nil){
            MSFootprintModel *model = self.tableVC.dataArray[_footerIndex];
            model.isBadEd = YES;
//            model.badNum = [NSString stringWithFormat:@"%ld",[model.badNum integerValue]+1];
            [self.tableVC.tableView reloadData];
        }
    }
}
#pragma mark DYTableViewProtocol
/** 上拉 */
- (void)pullTableView:(UITableView *)tableView isNextPage:(BOOL)isNextPage{
    self.tableVC.pageNum++;
    [self request:NO];
}

- (void)pullRefreshRequest
{
    self.tableVC.pageNum = 1;
    [self request:YES];
}

-(void)request:(BOOL)isPull
{
    UIView * hudView = isPull?nil:self.contentView;
    NSString *pageNumber = [NSString stringWithFormat:@"%d",self.tableVC.pageNum];
    NSDictionary *dict = @{
                           @"pageNo":pageNumber,
                           @"pageSize":@"10",
                           };
    [netEngine requestMyFootWithTarget:self.VctrID hudView:hudView params:dict];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)footprintClick:(FootprintClickType)type clickCellIndex:(NSInteger)index{
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
//    MSDetailFootprintCell *cell = [self.tableVC.tableView cellForRowAtIndexPath:indexPath];
    _footerIndex = index;
    
    MSFootprintModel *model = self.tableVC.dataArray[index];
    NSString *footerId = model.itemID;
    
    if (type == FootprintClickType_Image){
        NSMutableArray *commentImageArr = [[NSMutableArray alloc] init];
        for (int i = 0; i < model.imageArray.count; i++) {
            propertyPicture *pictureModel = (propertyPicture *)model.imageArray[i];
            [commentImageArr addObject:pictureModel.path];
        }
        
        MSPictureShowViewController *pictureShowVC = [[MSPictureShowViewController alloc] init];
        pictureShowVC.titleStr = @"";
        pictureShowVC.imageArr = commentImageArr;
        pictureShowVC.imageIndex = 0;
        [rootNavigation pushViewController:pictureShowVC animated:YES];
        return;
    }

    if (type == FootprintClickType_Delete) {
        NSString *remindLabelStr = @"是否确认删除该条足迹";
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:remindLabelStr message:nil delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"确认", nil];
        [alertView show];
        
    }
    if (type == FootprintClickType_Good) {
        [netEngine requestGoodFooterWithTarget:self.VctrID hudView:self.contentView footerId:footerId];
    }
    if (type == FootprintClickType_Bad) {
        [netEngine requestBadFooterWithTarget:self.VctrID hudView:self.contentView footerId:footerId];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        MSFootprintModel *model = self.tableVC.dataArray[_footerIndex];
        NSString *footerId = model.itemID;
        
        [netEngine requestDeleteFooterWithTarget:self.VctrID hudView:self.contentView footerId:footerId];
    }
}

- (void)reloadTableView{
    [self.tableVC.tableView reloadData];
}

@end
